import jieba 
import random
emotions=['anger','disgust','fear','joy','sadness']

with open(r'C:\Users\User\Desktop\code\pycode\lab2\weibo.txt','r',encoding='utf-8') as f:
    text=f.read().split('\n')
text=random.sample(text,5000)
#进行加入情感词典的jieba分割
def cut_new(line,emotions):#line为单句三部分
    text=[]
    line_cut=line.split('\t')#按 \t’分割
    text=line_cut[1]
    #jieba 分割
    text_word=[]
    #将情感词加入jieba的自定义词典
    for i in emotions:  
        jieba.load_userdict("C:\\Users\\User\\Desktop\\code\\pycode\\lab3\\emotion_lexicon\\"+i+".txt")
    text=jieba.cut(text)
    for i in text:
        text_word.append(i)
    #停用词表
    stop_word=[]
    with open(r'C:\Users\User\Desktop\code\pycode\baidu_stopwords.txt',encoding="utf-8") as fs:
            stop_word.append(fs.read().split('\n'))
    #去除停用词
    text_final=[]
    for txt in text_word:
        if txt not in stop_word:
            text_final.append(txt)
    return text_final

#情绪分析函数1
def emo_ana(emotions,cut_line,way):#way==1为情绪向量，way==2为情绪,cut_line为第一个函数切好的
    dic_name={}
    for names in emotions:
        with open("C:\\Users\\User\\Desktop\\code\\pycode\\lab3\\emotion_lexicon\\"+names+".txt",
                encoding="utf-8") as f :
            dic_name[names]=f.read().split('\n')#同一个情感类转换为字典
    
    dic={"anger":0,"disgust":0,"fear":0,"joy":0,"sadness":0}
    for names in emotions:
        for i in cut_line:#切好的单词
            if i in dic_name[names]:
                dic[names]+=1#累加次数
    total=sum(dic.values())#总情感词个数
    if total==0:
        return "neutral"
    elif total!=0:
        #情绪向量
        if way == 1: 
            def emo_vector():
                nonlocal dic
                vector=[x/total for x in dic.values()]#分量
                return vector
            return emo_vector
        #情绪
        elif way == 2:
            def emo():
                nonlocal dic_name,dic
                lis=[]
                for values in dic.values():
                    lis.append(values)
                
                m=max(lis) 
                ind=[] #索引表
                count=0
                for j in lis:
                    if j == m:
                        count+=1
                        ind.append(lis.index(j))
                if count==1:                  
                    i = lis.index(max(lis))
                else:
                    i=random.choice(ind)
                return emotions[i]
            return emo

'''
#按月分析生气的情绪
def time_anger(text):#text是按照换行符分割的句子，即三个部分,一共5000个
    emotions=['anger','disgust','fear','joy','sadness']
    time=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
    total=[0]*12
    for sen in text:#sen是单句
        mon=sen.split('\t')[2].split(' ')[1]
        for i in range(12):
            if time[i]==mon:
                print(sen)
                lis = emo_ana(emotions,cut_new(sen,emotions),2)()
                print(lis)
                if lis=='anger':
                    total[i]+=1#加上每一句的joy分量
    return total
'''

lis=cut_new("[23.116648, 113.254956]	很久没心情弄大餐，今晚雅兴！顺便发图！	Tue Oct 15 23:58:41 +0800 2013",emotions)
a= emo_ana(emotions,lis,1)()
print(a)

             
   
